package com.hejielife.singlethreadedexecution2;

/**
 * 2019年8月2日 上午10:20:42
 * 
 * @author hejielife
 * @despribe 判断门是否安全
 */
public class Main {
	public static void main(String[] args) {
		System.out.println("Testing SecurityGate...");
		SecurityGate gate = new SecurityGate();
		CrackerThread[] t = new CrackerThread[100];

		// CrackerThread开始执行
		for (int i = 0; i < t.length; i++) {
			t[i] = new CrackerThread(gate);
			t[i].start();
		}

		// CrackerThread停止
		for (int i = 0; i < t.length; i++) {
			try {
				t[i].join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// 判断是否安全
		if (gate.getCounter() == 0) {
			System.out.println("SecurityGate may be safe.");
		} else {
			System.out.println("SecurityGate is NOT safe!");
			System.out.println("getCounter() == " + gate.getCounter());
		}
	}
}
